<?php
include("model/products.php");
class productsController{
	
	function indexAction(){
		$smarty = $_SESSION['smarty'];
		
		$list_products_box = $this->getListProductBox();
		$smarty->assign("list_products_box",$list_products_box);
		
		$content = $smarty->fetch("products/products.html");		
		$smarty->assign("content", $content);
		
		$smarty->display("homepage.html");
	}
	
	function getListProductBox()
	{
		$smarty 		= $_SESSION['smarty'];
		$product_obj 	= new products();
		$productList	= $product_obj->getProductList();		
		$smarty->assign("productList", $productList);
		$html 		= $smarty->fetch("products/products_list.html");
		return $html;
	}
	
	
	function insertAction(){
		$products_obj = new products();
		
		$data 		= array();
		$data['pro_name'] 			= $_REQUEST['pro_name'];
		$data['pro_description'] 	= $_REQUEST['pro_description'];
		$data['pro_price'] 			= $_REQUEST['pro_price'];
	
		$data['pro_issuedate'] 		= $_REQUEST['pro_issuedate'];
		$data['pro_br_id'] 			= $_REQUEST['pro_br_id'];
		$data['pro_cat_id']			= $_REQUEST['pro_cat_id'];
	
		$result = $products_obj->insertAProduct($data);
		if ($result == 1)
			$msg = "insert successfully";
		else if ($result == -1)
			$msg = "duplicated!";
		else
			$msg = "insert failed.";
			
		$smarty = $_SESSION['smarty'];		
		$smarty->assign("msg", $msg);
		$content = $smarty->fetch("products/products_insertResult.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
		
	}
	
	function inactiveAction(){
		$pro_id 		= $_REQUEST['args'];
		$products_obj	= new products();
		$result 		= $products_obj->inactivateAProduct($pro_id);
		if ($result == 1)
			$msg = "inactivate successfully";		
		else
			$msg = "inactivate failed.";
			
		$smarty = $_SESSION['smarty'];		
		
		
		$list_products_box = $this->getListProductBox();
		$smarty->assign("list_products_box",$list_products_box);
		$smarty->assign("msg", $msg);
		
		$content = $smarty->fetch("products/products.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
	}
	
	function activeAction(){
		$pro_id 		= $_REQUEST['args'];
		$products_obj 	= new products();
		$result 		= $products_obj->activateAProduct($pro_id);
		if ($result == 1)
			$msg = "activate successfully";		
		else
			$msg = "activate failed.";
			
		$smarty = $_SESSION['smarty'];		
		
		
		$list_products_box = $this->getListProductBox();
		$smarty->assign("list_products_box",$list_products_box);
		$smarty->assign("msg", $msg);
		
		$content = $smarty->fetch("products/products.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
	}
	
	function insertFormAction(){
		$smarty = $_SESSION['smarty'];		
		
		$brandsList = array();
		include("model/brands.php");
		$brands_obj = new brands();
		$brandsList = $brands_obj->getBrandsList();
		
		$categoriesList = array();
		include("model/categories.php");
		$categories_obj = new categories();
		$categoriesList = $categories_obj->getCategoriesList();
		
		$smarty->assign("categoriesList", $categoriesList);
		$smarty->assign("brandsList", $brandsList);
		$insertform_products_box = $smarty->fetch("products/products_insert_form.html");
		
		$smarty->assign("insertform_products_box",$insertform_products_box);
		
		$content = $smarty->fetch("products/products.html");		
		$smarty->assign("content", $content);
		
		$smarty->display("homepage.html");
	}
	
	function editFormAction(){
		$pro_id 			= $_REQUEST['args'];
		$smarty 		= $_SESSION['smarty'];		
		
		$brandsList = array();
		include("model/brands.php");
		$brands_obj = new brands();
		$brandsList = $brands_obj->getBrandsList();
		
		$categoriesList = array();
		include("model/categories.php");
		$categories_obj = new categories();
		$categoriesList = $categories_obj->getCategoriesList();
		
		$smarty->assign("categoriesList", $categoriesList);
		$smarty->assign("brandsList", $brandsList);
		
		$products_obj 		= new products();
		$current_data 		= array();
		$current_data 		= $products_obj->getProductsInfo($pro_id);
		
		$smarty->assign("current_data", $current_data);
		
		$editform_products_box = $smarty->fetch("products/products_edit_form.html");
		$smarty->assign("editform_products_box",$editform_products_box);
		
		$content = $smarty->fetch("products/products.html");		
		$smarty->assign("content", $content);
		
		$smarty->display("homepage.html");	
	}
	
	function editAction(){
		$products_obj = new products();
		$data 		= array();
		$data['pro_id'] 			= $_REQUEST['pro_id'];
		$data['pro_name'] 			= $_REQUEST['pro_name'];
		$data['pro_description'] 	= $_REQUEST['pro_description'];
		$data['pro_price'] 			= $_REQUEST['pro_price'];
		$data['pro_issuedate'] 		= $_REQUEST['pro_issuedate'];		
		$data['pro_br_id'] 			= $_REQUEST['pro_br_id'];
		$data['pro_cat_id'] 		= $_REQUEST['pro_cat_id'];
		
		$result = $products_obj->editAProduct($data);
		
		if ($result == 1)
			$msg = "edit successfully";
		else
			$msg = "update failed.";
			
		$smarty = $_SESSION['smarty'];		
		$smarty->assign("msg", $msg);
		$content = $smarty->fetch("products/products_updateResult.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
	}
	
}
?>